<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );

class EcommerceModelOrders extends JModel
{
	function __construct()
	{
		parent::__construct();
	}
	
	function getOrderDetail($order_id){
		$query = "SELECT * FROM #__pr_orders o LEFT JOIN #__users u ON o.order_user_id = u.id LEFT JOIN #__users_extension ue ON o.order_user_id = ue.id WHERE o.order_id  = ". $order_id ;
		$this->_db->setQuery($query);	
		if($order = $this->_db->loadObject()){
			$query = "SELECT c.*, p.number FROM #__pr_cart c LEFT JOIN #__pr_product p ON c.product_id = p.id WHERE c.order_id  = " . $order_id . "  ORDER BY p.id";
			$this->_db->setQuery( $query );
			$order->products = $this->_db->loadObjectList();
		}
		return $order;
	}
	
	function getList()
	{	
		$user = JFactory::getUser();
		$query = "SELECT * FROM #__pr_orders WHERE order_user_id  = " . $user->id . "  ORDER BY order_date DESC ";
		
		$this->_db->setQuery($query);	
		$orders = $this->_db->loadObjectList();
		
		$i = 0;
		foreach($orders as $order){
			$query = "SELECT cart.*, p.id, p.name, p.number, com.name as com_name, p.type, p.unit, p.parcel FROM #__pr_cart AS cart 
					LEFT JOIN #__pr_product AS p ON cart.product_id = p.id 
					LEFT JOIN #__pr_company AS com ON p.manufacturer = com.id 
					WHERE cart.order_id = ".$order->order_id;
			$this->_db->setQuery($query);
			$orders[$i]->cart = $this->_db->loadObjectList();
			$i++;
		}

		return $orders;	
	}
}
?>